<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/dbConnection.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/category.php';

class CategoryDao {
	private $_db;

	public function __construct() {
		$this->_db = new DbConnection();
		$this->_db->connect();
	}

	public function getCategoryProviderList($parentID) {
		$query = "SELECT * FROM categories_provider WHERE ParentId = '" . $parentID . "'";
				
		$result = $this->_db->executeQuery($query);

		if (!$result) {
		    echo 'Could not run query: ' . ' ' . mysql_error();
		    return;
		}
		
		$categoryList = array();
		
		while($row = mysql_fetch_assoc($result)) {			
			$category = new CategoryModel();
			$category->setCategoryID($row['CategoryId']);
			$category->setCategory($row['Category']);
			$category->setDescription($row['Description']);
			$category->setParentID($row['ParentId']);
						
			$categoryList[] = $category;
		}		
		
		mysql_free_result($result);		
		
		return $categoryList;
	}
	
	public function getCategoryProviderFromID($catID) {
		$query = "SELECT * FROM categories_provider WHERE CategoryId = '" . $catID . "'";
		
		$result = $this->_db->executeQuery($query);
		
		if ($row = mysql_fetch_assoc($result)) {			
			$cat = new CategoryModel();
			$cat->setCategory($row['Category']);
			$cat->setCategoryID($row['CategoryId']);
			$cat->setDescription($row['Description']);
			$cat->setParentID($row['ParentId']);			
		}
		
		if (!$result) {
			mysql_free_result($result);
		}
		
		return $cat;
	}
	
	public function getCategoryItemList($parentID) {
		$query = "SELECT * FROM categories_item WHERE ParentId = '" . $parentID . "'";
				
		$result = $this->_db->executeQuery($query);

		$categoryList = array();		
		while ($row = mysql_fetch_assoc($result)) {			
			$category = new CategoryModel();
			$category->setCategoryID($row['CategoryId']);
			$category->setCategory($row['Category']);
			$category->setDescription($row['Description']);
			$category->setParentID($row['ParentId']);
						
			$categoryList[] = $category;
		}
		
		if ($result) {
			mysql_free_result($result);
		}

		return $categoryList;
	}
	
	public function getCategoryItemFromID($catID) {
		$query = "SELECT * FROM categories_item WHERE CategoryId = '" . $catID . "'";
		
		$result = $this->_db->executeQuery($query);
		
		if ($row = mysql_fetch_assoc($result)) {			
			$cat = new CategoryModel();
			$cat->setCategory($row['Category']);
			$cat->setCategoryID($row['CategoryId']);
			$cat->setDescription($row['Description']);
			$cat->setParentID($row['ParentId']);			
		}
		
		if (!$result) {
			mysql_free_result($result);
		}
		
		return $cat;
	}	
}
?>